package com.wondedDream.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.wondedDream.model.*;
import com.wondedDream.repository.CourseCategoryRepository;
import com.wondedDream.repository.MockExamRepository;
import com.wondedDream.repository.PracticeQuestionRepository;
import com.wondedDream.repository.PracticeRepository;
import com.wondedDream.service.PracticeQuestionService;
import com.wondedDream.util.HtmlCrawler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PracticeQuestionServiceImpl implements PracticeQuestionService {
    @Autowired
    private PracticeQuestionRepository practiceQuestionRepository;
    @Autowired
    private CourseCategoryRepository courseCategoryRepository;
    @Autowired
    private PracticeRepository practiceRepository;
    @Autowired
    private HtmlCrawler htmlCrawler;

    @Override
    public PracticeQuestion savePracticeQuestion(PracticeQuestion question) {
        return practiceQuestionRepository.save(question);
    }

    @Override
    public PracticeQuestion getPracticeQuestionById(Integer id) {
        return practiceQuestionRepository.findById(id).orElse(null);
    }

    @Override
    public List<PracticeQuestion> getPracticeQuestionsByPracticeId(Integer practiceId) {
        return practiceQuestionRepository.getPracticeQuestionsByPracticeId(practiceId);
    }

    @Override
    public void deletePracticeQuestion(Integer id) {
        practiceQuestionRepository.deleteById(id);
    }

    @Override
    public PracticeQuestion updatePracticeQuestion(PracticeQuestion practiceQuestion) {
        return practiceQuestionRepository.saveAndFlush(practiceQuestion);
    }

    @Override
    public void createMockExamQuestionByFileJson(String localPath,Integer courseCategoryId,String practiceName) {
        Gson gson = new Gson();
        String practiceQuestionJsonFile = htmlCrawler.parselxJsonFile(localPath);
        List<PracticeQuestion> questionList = new ArrayList<>();

        CourseCategory courseCategory = new CourseCategory();
        courseCategory.setId(courseCategoryId);

        Practice practice = new Practice();
        practice.setPracticeName(practiceName);
        practice.setCategory(courseCategory);
        practice.setCreateTime(new Date());
        Practice practice1 = practiceRepository.save(practice);

        JsonArray jsonArray = gson.fromJson(practiceQuestionJsonFile, JsonArray.class);
        for (JsonElement element : jsonArray) {
            JsonObject jsonObject = element.getAsJsonObject();
            String questionContent = jsonObject.get("question").getAsString();
            String correctAnswer = jsonObject.get("correctAnswer").getAsString();
            String analysis = jsonObject.get("analysis").getAsString();
            JsonArray optionsArray = jsonObject.getAsJsonArray("options");
            String optionA = optionsArray.get(0).getAsJsonObject().get("content").getAsString();
            String optionB = optionsArray.size() > 1? optionsArray.get(1).getAsJsonObject().get("content").getAsString() : "";
            String optionC = optionsArray.size() > 2? optionsArray.get(2).getAsJsonObject().get("content").getAsString() : "";
            String optionD = optionsArray.size() > 3? optionsArray.get(3).getAsJsonObject().get("content").getAsString() : "";
            PracticeQuestion questionObj = null;
            if(correctAnswer.length() > 1){
                questionObj = new PracticeQuestion(practice1, questionContent,optionA,optionB, optionC, optionD,  correctAnswer, analysis, MockExamQuestion.QuestionType.MULTIPLE_CHOICE);
            }else {
                questionObj = new PracticeQuestion(practice1, questionContent,optionA,optionB, optionC, optionD,  correctAnswer, analysis, MockExamQuestion.QuestionType.SINGLE_CHOICE);
            }
            questionList.add(questionObj);
        }
        practiceQuestionRepository.saveAll(questionList);
    }
}